home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / chat / ircii-2.8he / ircii-2 / help / expressions < prev    next >
Encoding:
Text File  |  1993-05-04  |  6.7 KB  |  185 lines

  1.   This help file describes expresssions available in ${}, and the
  2.   @, IF, WHILE, and FOREACH commands.
  3.  
  4.   Variable expressions are used to perform arithmetic, string and
  5.   boolean operations. Their syntax is similar to the syntax of most
  6.   computer languages, and is modelled on C syntax. A simple example
  7.   of this, is that when ${A + 2} is used, and the ASSIGNed variable
  8.   A has the value '3', the result is 5.
  9.  
  10.   Brackets can be used in these expressions, so (A+2)*3 would give a
  11.   value of (3+2)*3 == 15, whereas A+2*3 would give a value of 9, because
  12.   multiplication is performed before addition.
  13.  
  14.   The common arithmetic operations (+, -, *, /) are all available.
  15.   Additionally, there is a string concatenation operator (##).
  16.   This treats both sides as strings and pastes them together. Thus
  17.   if A is 3 and B is 7, "A##B" becomes "37", but "A+B" becomes 10.
  18.  
  19.   You can also assign values to a variable in these expressions
  20.   using the '=' operator. Thus if you have the expression "C = A+B",
  21.   it will assign the value 10 to C. This has a number of affects.
  22.   The most common case is the @ command, where you can enter this expression
  23.   literally:
  24.  
  25.     @ C = A + B
  26.  
  27.   The '=' operator returns the value assigned. Thus you can extend
  28.   this to:
  29.  
  30.     @ D = C = A + B
  31.  
  32.   Which will assign the value 10 to both C and D. In a $ expression,
  33.   you might want to assign a value to a variable, and display it at the
  34.   same time. Thus you might do the following:
  35.  
  36.     echo The value of C is now ${C = A+B}
  37.  
  38.   which would set C to 10 and display "The value of C is now 10".
  39.  
  40.   Comparison operations also exist. These return 1 if the comparison is
  41.   valid (true) and 0 if it is invalid (false). The comparison operations
  42.   available are:
  43.  
  44.     == (equal to)
  45.     >  (greater than)
  46.     >= (greater than or equal to)
  47.     <  (less than)
  48.     <= (less than or equal to)
  49.     != (not equal to)
  50.  
  51.   Thus, the following expressions would result in the following results:
  52.    ( recall A = 3 and B = 7 )
  53.  
  54.     A == B            FALSE    0
  55.     A == 3            TRUE    1
  56.     A > 3            FALSE    0
  57.     A >= 3            TRUE    1
  58.     A != 3            TRUE    1
  59.  
  60.   These expressions can be combined with || (OR), && (AND) and ^^ (XOR).
  61.   So (A == 3) || (B==3) would be TRUE, and (A == 2) && (B == 7) would be
  62.   FALSE. You can also negate expressions with ! (NOT), so !(A == 3) would
  63.   be FALSE. These boolean expressions are used primarily in IF and WHILE.
  64.  
  65.   A string expression is considered to be true if it is non empty. So
  66.   if E has the value "word", then E || ( A > 3) is true, because E has
  67.   a non empty value, and !E is false.
  68.  
  69.   Functions can also appear in expressions. The arguments to functions
  70.   are evaluated as normal $ type expressions. Thus the following alias:
  71.  
  72.     alias test echo ${ MID(3 2 $0) + 5) }
  73.  
  74.   is executed with /test 123456, would give  34 (Two digits from position 3
  75.   in $0) + 5, giving 39. If the function returns another function name, that
  76.   function name can be called with another set of brackets. Thus if you have
  77.   the following set of aliases:
  78.  
  79.     alias fptr
  80.     {
  81.         if ( [$0] )
  82.             { @ function_return = func1 }
  83.             { @ function_return = func2 }
  84.     }
  85.     alias func1 @ function_return = this is $0
  86.     alias func2 @ function_return = that is $0
  87.     alias check echo ${ fptr($0)($1) }
  88.  
  89.   then /check 0 1 would print "that is 1", and /check 1 0 would print
  90.   "this is 0".
  91.  
  92.   Array expressions can also be used in this way. For example:
  93.  
  94.     assign A.1.1 One One was a racehorse
  95.     assign A.1.2 Two Two was one too
  96.     assign A.2.1 One One won one race
  97.     assign A.2.2 Two Two won one too
  98.     alias rhyme echo ${ A[$0][$1] }
  99.  
  100.   would cause /rhyme 2 1 to print "One One won one race".
  101.  
  102.   Function and array expressions can be combined to give a form
  103.   of pointer arithmetic like that seen in C. If a function
  104.   RhymeNum exists as follows:
  105.  
  106.     alias RhymeNum @ function_return = [A]
  107.   and rhyme is changed to:
  108.     alias rhyme echo ${ RhymeNum()[$0][$1] }
  109.  
  110.   /rhyme 2 1 still prints "One One won one race". This is because
  111.   RhymeNum returns A, and the expression then becomes A[$0][$1].
  112.   The $0 is expanded to 2, so it becomes A.2[$1], and the $1 is
  113.   expanded to 1, so it becomes A.2.1, which is then substituted as
  114.   a variable for "One One won one race". The reverse is also possible.
  115.   For example, a robot might have the following:
  116.  
  117.      alias thing.0 @ function_return = laughs his silly head off
  118.      alias thing.1 @ function_return = growls menacingly
  119.      alias thing.2 @ function_return = smiles like a crocodile
  120.      alias thing.3 @ function_return = wails uncontrollably
  121.      alias something SAY WereBot ${ thing[$RANDOM(4)]() }
  122.  
  123.   Will cause WereBot to say that it's laughing itself silly, growling
  124.   menacingly, smiling like a crocodile or wailing uncontrollably.
  125.   Assuming RANDOM(4) results in a value of 2, This expands to
  126.   thing.2(), which is then considered to be a function and substituted
  127.   to "smiles like a crocodile", the end result being that WereBot will
  128.   say "WereBot smiles like a crocodile".
  129.  
  130.   Finally, there are occasions when you need to get back to the $
  131.   substitution level. This can be done by enclosing text to be used at
  132.   this level in [..]. For example, [A] substitutes to a literal 'A', 
  133.   whereas A on its own substitutes to 3, and [$0] is needed to get
  134.   argument 0, because 0 on its own is taken to be the number 0.
  135.   Example:
  136.  
  137.     alias something SAY WereBot ${ [$0][$RANDOM(4)]() }
  138.  
  139.   will case /something thing to first expand [$0] to thing, giving
  140.   thing[$RANDOM(4)](), and if $RANDOM(4) returns 1, this becomes
  141.   thing.1(), which expands to "growls menacingly", and causes
  142.   WereBot to say "WereBot growls menacingly".
  143.  
  144.  
  145.   The following is the parse tree for expressions such as  those
  146.  
  147.     NU_EXPR = NU_CONJ
  148.     NU_CONJ = NU_CONJ && NU_CONJ    |
  149.           NU_CONJ || NU_CONJ    |
  150.           NU_CONJ ^^ NU_CONJ    |
  151.           NU_ASSN
  152.     NU_ASSN = varexp = NU_ASSN    |
  153.           NU_COMP
  154.     NU_COMP = NU_COMP == NU_COMP    |
  155.           NU_COMP != NU_COMP    |
  156.           NU_COMP >  NU_COMP    |
  157.           NU_COMP >= NU_COMP    |
  158.           NU_COMP <  NU_COMP    |
  159.           NU_COMP <= NU_COMP    |
  160.           NU_ADD
  161.     NU_ADD  = NU_ADD + NU_ADD    |
  162.           NU_ADD - NU_ADD    |
  163.           NU_ADD ## NU_ADD    |
  164.           NU_MULT
  165.     NU_MULT = NU_MULT * NU_MULT    |
  166.           NU_MULT / NU_MULT    |
  167.           NU_UNIT
  168.     NU_UNIT = token NUX_MODIF    |
  169.          unaryop token        |
  170.         ( NU_EXPR )        |
  171.         [ expression ] NUX_MODIF
  172.  
  173.     NUX_MODIF = ( expression ) NUX_MODIF |
  174.             [ expression ] NUX_MODIF
  175.  
  176. Special cases:
  177.   If a (...) or {...} construct is quoted with \(...\) or \{...\}
  178.   then variable expansion will take place on the first available
  179.   parse run.  However, after parsed, the \'s are eaten. e.g.
  180.     alias bonk echo ($0)        will return "($*)" regardless of
  181.   the argument to the alias.  No expansion takes place.  However, in
  182.     alias bonk echo \($0\)
  183.   expansion takes place as the special meaning of the () is taken
  184.   away by \.  This will return "(arguments to bonk)".
  185.